#ifndef OMG_DDS_CORE_REFERENCE_TYPE_HPP_
#define OMG_DDS_CORE_REFERENCE_TYPE_HPP_

/* Copyright (c) 2009-2010, Real-Time Innovations, Inc.
 * Copyright (c) 2010, Object Management Group, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - Neither the names of the above copyright holders nor the names of their
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL A COPYRIGHT HOLDER OR CONTRIBUTOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


namespace dds { namespace core {

#define OMG_DDS_REFERENCE_TYPE(TYPE) \
  public:\
    TYPE(); \
    TYPE(const TYPE& src); \
    ~TYPE();\
  public:\
    TYPE& operator=(const TYPE& src);\
    virtual TYPE& operator=(const dds::core::null_type);


/**
 * The <code>Reference</code> class defines the abstract behavior for those
 * type defined as "reference-type" in the DDS-PSM-CXX specification.
 */
template <typename DELEGATE>
class Reference
{
protected:
    /**
     * Creates a "null" reference.
     */
    Reference();

    /**
     * Creates a reference from another.
     *
     * @param ref the other reference
     */
    Reference(const Reference& ref);

public:
    /**
     * Destroys a reference.
     */
    ~Reference();


public:
    /**
     * Compares two reference objects and returns true if they are equal.
     * Equality is based on the referential equality of the object being
     * pointed.
     *
     * @param ref the other reference object
     */
    template <typename R>
    bool operator==(const R& ref) const;

    /**
     * Compares two reference objects and returns true if they are not-equal.
     * Inequality is based on the referential inequality of the object being
     * pointed.
     *
     * @param ref the other reference object
     */
    template <typename R>
    bool operator!=(const R& ref) const;

    /**
     * Special assignment operators that takes care of assigning <i>null</i>
     * to this reference. When assigning null, there might be an associated
     * garbage collection activity.
     *
     * In essence this assignment operators takes care of allowing programmers
     * to write:
     *    MyRefType my_ref = //...
     *    my_ref = dds::null;
     */
    Reference&
    operator=(const null_type);

    /**
     * Returns true if this reference object is nil, meaning pointing to null.
     */
    bool is_nil() const;

    /**
     * Special <code>operator==</code> used to check if this reference object
     * equals the <code>null</code> reference.
     * The null-check can be done like this:
     * <code>
     *    bool is_null = (r == dds::null);
     * </code>
     * If r is a null reference the <code> is_null</code> variable will
     * have the <b>true</b> value.
     *
     * @return true if this reference is null.
     */
    bool
    operator==(const null_type) const;

    /**
     * Special <code>operator!=</code> used to check if this reference object
     * does not equals the <code>null</code> reference.
     * The non-null-check can be done like this:
     * <code>
     *    bool is_null = (r != dds::null);
     * </code>
     * If r is a non-null reference the <code> is_null</code> variable will
     * have the <b>false</b> value.
     *
     * @return true if this reference is null.
     */
    bool operator!=(const null_type nil) const;

private:
    // -- disallow dynamic allocation for reference types
    void* operator new(size_t);



protected:
    DELEGATE impl_;
};


} } /* namespace dds / namespace core */

#endif /* OMG_DDS_CORE_REFERENCE_TYPE_HPP_ */

